package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.common.IdWorker;
import cn.cs.pojo.UserCompany;
import cn.cs.service.UserCompanyService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/userCompany")
public class UserCompanyController {
    @Resource
    private UserCompanyService userCompanyService;

    /****
     * 保存用户企业地址
     * @param userCompany
     * @param
     * @return
     */
    @RequestMapping("/wxApi/addUserCompany")
    public Dto addUserCompany(@RequestBody UserCompany userCompany) throws Exception {

        try {
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(userCompany.getId())) {
                userCompany.setId(IdWorker.getId());
                userCompany.setCreateTime(new Date());
                userCompanyService.addUserCompany(userCompany);
            } else {
                msg = "修改成功";
                userCompany.setUpdateTime(new Date());
                userCompanyService.modifyUserCompany(userCompany);
            }
            return DtoUtil.returnSuccess(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

        /****
         * 查寻用户企业地址信息
         * @param userId
         * @param
         * @return
         */
        @RequestMapping("/wxApi/getUserCompany")
        public Dto getUserCompany(String userId, String addressTypeId) throws Exception {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("userId", userId);
            map.put("addressTypeId", addressTypeId);
            if(EmptyUtils.isNotEmpty(addressTypeId)){
                List<UserCompany> list = userCompanyService.getUserCompanyListByMap(map);
                if(list.size() > 0){
                    return DtoUtil.returnSuccess("获取成功", list.get(0));
                }
                return DtoUtil.returnSuccess("获取成功", new HashMap<>());
            }else{
                return DtoUtil.returnSuccess("获取成功", userCompanyService.getUserCompanyListByMap(map));
            }

        }

      /*
        * 删除用户企业地址信息
        * */
        @RequestMapping("/wxApi/delUserCompany")
        @ResponseBody
        public Dto delUserCompany(String id){
            try {
                userCompanyService.deleteUserCompanyById(id);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("操作失败","0002");
            }
            return DtoUtil.returnSuccess("操作成功", "");
        }

        /*
         * 设为默认
         *
         * */
        @RequestMapping("/wxApi/updateAddressTypeId")
        @ResponseBody
        public Dto updateAddressTypeId(String id)throws Exception{
            return DtoUtil.returnSuccess("操作成功", userCompanyService.updateAddressTypeId(id));
        }
    }








